package org.bronycorps.mlptss.domain.controller;

import org.bronycorps.mlptss.domain.model.Player;
import org.bronycorps.mlptss.manager.ai.IArtificialPlayer;
import org.bronycorps.mlptss.manager.ai.PredictionManager;
import org.bronycorps.mlptss.manager.ai.playerimpl.BasicPlayer;
import org.bronycorps.mlptss.manager.preferences.GameConfiguration;

import com.badlogic.gdx.math.Vector2;

public class PlayerComputer extends AbstractPlayerController {

	private IArtificialPlayer aiManager = null;
	private GameConfiguration gameConfiguration;
	private PredictionManager predictionManager = null;

	public PlayerComputer(Player player) {
		super(player);
		this.aiManager = new BasicPlayer(player);
		this.gameConfiguration = GameConfiguration.getInstance();
	}

	@Override
	public void act(float delta) {
		super.act(delta);

		if (this.predictionManager != null) {
			// compute values
			float speed = this.model.getMaxSpeed()
					* this.gameConfiguration.getInputSpeedScale();
			;
			// float speed = delta * this.model.getMaxSpeed() * 100;
			Vector2 nextPosition = this.aiManager.getNextPosition(getModel()
					.getPosition(), getModel().getHitbox(),
					this.predictionManager, delta, speed);

			Vector2 acceleration = nextPosition.cpy().sub(
					getModel().getPosition());

			if (acceleration.len() > 0) {
				this.model.setAngle(acceleration.angle());
			} else {
				this.model.setAngle(0);
			}

			this.position.set(nextPosition);
			// Gdx.app.log(MlpTss.LOG, "delta : " + delta);

			if (this.aiManager.isEnemyOnRange()) {
				this.model.fireShootEvent();
			}

			this.aiManager.update(delta);
		}

	}

	public IArtificialPlayer getAiManager() {
		return this.aiManager;
	}

	public PredictionManager getPredictionManager() {
		return this.predictionManager;
	}

	public void setPredictionManager(PredictionManager predictionManager) {
		this.predictionManager = predictionManager;
	}

}